<!--
 * @Author: maqaing
 * @Date: 2021-10-22 14:14:51
 * @LastEditors: mjj
 * @LastEditTime: 2021-10-27 17:04:18
 * @Description: 
-->
<template>
  <div class="production">
    <!-- 按钮区域 -->
    <div class="buttons">
      <el-button @click="`toAddHandler`" type="primary" size="small"
        >添加</el-button
      >
    </div>

    <!-- 表格区域 -->
    <el-table :data="tabelData.list" size="small">
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="id" label="产品编号"></el-table-column>
      <el-table-column prop="name" label="产品名称"></el-table-column>
      <el-table-column label="产品图片">
        <!-- 插槽 -->
        <template v-slot="scope">
          <el-popover
            placement="right"
            width="200"
            trigger="hover"
            content="这是一段内容"
          >
            <img :src="scope.row.photo" height="120" width="160" />
            <img slot="reference" :src="scope.row.photo" alt="" height="50px" />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="产品价格"> </el-table-column>
      <el-table-column label="产品状态">
        <!-- 插槽 -->
        <template v-slot="scope">
          <el-tag
            v-if="scope.row.status == '正常'"
            size="mini"
            type="success"
            >{{ scope.row.status }}</el-tag
          >
          <el-tag v-else size="mini" type="danger">{{
            scope.row.status
          }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="category.name" label="所属栏目"></el-table-column>
      <el-table-column label="操作" align="center">
        <!-- 插槽 -->
        <template v-slot="scope">
          <el-button type="text" size="mini" style="color: green"
            >编辑</el-button
          >
          <el-button
            v-if="scope.row.status == '正常'"
            type="text"
            size="mini"
            style="color: red"
            @click="unDownProductHandler(scope.row.id, $event)"
            >下架</el-button
          >
          <el-button
            v-else
            type="text"
            size="mini"
            @click="unDownProductHandler(scope.row.id, $event)"
            >上架</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页插件区域 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="tabelData.page"
      :page-sizes="[5, 8, 10, 15]"
      :page-size="tabelData.pageSize"
      layout="total, sizes, prev, pager, next"
      :total="tabelData.total"
    >
    </el-pagination>
    <!-- 模态框区域 -->.
    <el-dialog title="title" :visible.sync="Visible">
      <el-form :model="product"  ref="prmForm">
        <el-form-item
          label="产品名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input
            v-model="product.name"
            placeholder="请输入产品名称"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="所属栏目"
          prop="productCategoryId"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="product.producCategoryId"
            placeholder="请选择所属栏"
          >
            <el-option
              v-for="(item, index) in productCategories"
              :key="index"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from "@/utils/request";
// 导入辅助函数
import { mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      tabelData: {},
      queryParams: {
        page: 1,
        pageSize: 5,
      },
      // 模态框的显示与隐藏
      Visible: false,
      formLabelWidth: "80px",
      product:[]
    };
  },
  computed: {
    ...mapState("productCategory", ["productCategories"]),
  },
  methods: {
    ...mapActions("productCategory", ["findAllProductCategoies"]),

    // 1.分页查询产品信息
    pageQueryProductions() {
      request
        .get("/product/pageQuery", {
          params: {
            page: this.queryParams.page,
            pageSize: this.queryParams.pageSize,
          },
        })
        .then((res) => {
          this.tabelData = res.data;
        });
    },

    // 分页size-change事件
    handleSizeChange(pageSize) {
      // 点击分页容量，将最新的pageSize设置到数据模型中
      this.queryParams.pageSize = pageSize;
      // 重新调用分页查询的方法，做数据重载
      this.pageQueryProductions();
    },
    // 分页current-page-change事件
    handleCurrentChange(page) {
      // 点击分页容量，将最新的page设置到数据模型中
      this.queryParams.page = page;
      // 重新调用分页查询的方法，做数据重载
      this.pageQueryProductions();
    },
    // 商品的上下架功能
    unDownProductHandler(id, e) {
      if (e.target.innerText == "上架") {
        this.$confirm("此操作将上架该产品, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          request
            .get("/product/online", {
              params: { id: id },
            })
            .then((res) => {
              // 重载数据
              this.pageQueryProductions();
              // 提示成功上架的信息
              this.$message({
                showClose: true,
                message: res.message,
                type: "success",
              });
            });
        });
      } else {
        this.$confirm("此操作将下架该产品, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          request
            .get("/product/offline", {
              params: { id: id },
            })
            .then((res) => {
              // 重载数据
              this.pageQueryProductions();
              // 提示成功下架的信息
              this.$message({
                showClose: true,
                message: res.message,
                type: "success",
              });
            });
        });
      }
    },
    // 添加按钮点击事件
    toAddHandler() {
      this.visible = true; //打开模态框
    },
  },
  created() {
    this.pageQueryProductions(); //调用分页查询
    this.findAllProductCategoies(); //调用查询所有栏目信息
  },
};
</script>
<style scoped>
</style>